package com.hyk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hyk.domain.AdminBank;
import com.hyk.dto.AdminBankDto;
import com.hyk.dto.R;
import com.hyk.fegin.AdminBankFegin;
import com.hyk.service.AdminBankService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

@RestController
@Api(tags = "公司银行卡控制器")
@RequestMapping("/adminBanks")
public class AdminBankController implements AdminBankFegin {

    @Resource
    private AdminBankService adminBankService;

    @GetMapping
    @ApiOperation("根据卡号查询银行卡列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankCard",value = "银行卡号"),
            @ApiImplicitParam(name = "current",value = "当前页数"),
            @ApiImplicitParam(name = "size",value = "每页大小")
    })
    @PreAuthorize("hasAuthority('admin_bank_query')")
    public R<Page<AdminBank>> list(@ApiIgnore Page<AdminBank> page, String bankCard){
        QueryWrapper<AdminBank> wrapper = new QueryWrapper<>();
        if (bankCard != null && bankCard != ""){
            wrapper.like("bank_card",bankCard);
        }
        wrapper.orderByDesc("name");
        return R.ok(adminBankService.page(page,wrapper));
    }

    @PostMapping
    @ApiOperation("新建银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "bankCard",value = "银行卡号"),
            @ApiImplicitParam(name = "bandName",value = "开户行（包含支行名称）"),
            @ApiImplicitParam(name = "coinId",value = "币种id"),
            @ApiImplicitParam(name = "coinName",value = "币种名称"),
            @ApiImplicitParam(name = "name",value = "开户人姓名"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_create')")
    public R add(@RequestBody @Validated AdminBank adminBank){
        boolean save = adminBankService.save(adminBank);
        if (save){
            return R.ok("新增成功");
        }
        return R.fail("新增失败");
    }

    @PatchMapping
    @ApiOperation("编辑银行卡")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "bankCard",value = "银行卡号"),
            @ApiImplicitParam(name = "bandName",value = "开户行（包含支行名称）"),
            @ApiImplicitParam(name = "coinId",value = "币种id"),
            @ApiImplicitParam(name = "coinName",value = "币种名称"),
            @ApiImplicitParam(name = "name",value = "开户人姓名"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R update(@RequestBody @Validated AdminBank adminBank){

        boolean update = adminBankService.updateById(adminBank);
        if (update){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @PostMapping("/adminUpdateBankStatus")
    @ApiOperation("修改银行卡状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "status",value = "状态")
    })
    @PreAuthorize("hasAuthority('admin_bank_update')")
    public R changeStatus(String bankId, Byte status){
        AdminBank adminBank = new AdminBank();
        adminBank.setId(Long.parseLong(bankId));
        adminBank.setStatus(status);
        boolean update = adminBankService.updateById(adminBank);
        if (update){
            return R.ok("修改成功");
        }
        return R.fail("修改失败");
    }

    @Override
    @GetMapping("/getAdminBank")
    public AdminBankDto getAdminUser() {
        QueryWrapper<AdminBank> wrapper = new QueryWrapper<>();
        wrapper.eq("status",1);
        List<AdminBank> list = adminBankService.list(wrapper);
        if (CollectionUtils.isEmpty(list)){
            return null;
        }
        AdminBank adminBank = list.get(0);
        AdminBankDto adminBankDto = new AdminBankDto();
        BeanUtils.copyProperties(adminBank,adminBankDto);
        return adminBankDto;
    }
}
